<?php

class Login extends Controller {

    function Login() {
        parent::Controller();
        //se incarca modelul
        $this->load->model("model_login");
        $this->load->model('model_usersession');
        //redirect daca este logat si ajunge pe acesta pagina
        if ($this->usersession->userLoggedIn()) {
            redirect("main", "refresh");
        }
    }


    /**
     * Incarcare pagina login
     */
    function index() {
        $this->load->view('login/loginform');
    }


    /**
     * Logare
     */
    function xlogin() {

        //creare mesaj default
        $aResponse = array("error" => TRUE, "type" => 'validation', "description" => "Utilizator sau parola incorecte!");

        $sUserName = $this->input->post('username');
        $sPsswd = $this->input->post('password');

        //verificare login
        $oUser = $this->model_login->checkUserPassword($sUserName, $sPsswd);

        if ($oUser !== FALSE) {

            //creare backup la baza de date
            //$this->model_login->backup();

            if ($this->usersession->accountConnected($oUser['user_id'])) {
                if ($this->input->post('kickuser') == '1') {
                    $this->model_usersession->deleteSession($oUser['user_id']);
                    $this->usersession->loginUser($oUser);
                    $aResponse = array("error" => FALSE, "type" => 'validation', "description" => "Autentificare reusita.");
                }
                else {
                    $aResponse = array("error" => TRUE, "type" => 'multiplesession', "description" => "Acest utilizator este deja conectat!<br />Doriti sa continuati oricum?<br /><i>(utilizatorul curent va fi delogat)</i>");
                }
            }
            else {
                $this->usersession->loginUser($oUser);
                $aResponse = array("error" => FALSE, "type" => 'validation', "description" => "Autentificare reusita.");
            }
        }
        echo json_encode($aResponse);
    }
}

?>